package com.hspedu.tankgame4;


import java.util.TreeMap;
import java.util.Vector;

/**
 * @author LiuJinTao
 * @className Enemy
 * @date 2024/06/21 9:38
 * @description 敌人坦克
 */
public class Enemy extends Tank implements Runnable {
    // 定义一个 Shot 对象，表示一个射击（线程）
    Shot shot = null;
    Vector<Shot> shots = new Vector<>();

    public Enemy(int x, int y, int direct) {
        super(x, y, direct);
    }

    public void shotBullet() {
        // 创建 Shot 对象，根据当前 tank 的位置和方向创建子弹位置
        switch (getDirect()) {
            case 0:// 向上
                shot = new Shot(getX()+20, getY(), 0);
                break;
            case 1:// 向右
                shot = new Shot(getX()+60, getY()+20, 1);
                break;
            case 2:// 向下
                shot = new Shot(getX()+20, getY()+60, 2);
                break;
            case 3:// 向左
                shot = new Shot(getX(), getY()+20, 3);
                break;
        }
        shots.add(shot);
        new Thread(shot).start();
    }

    @Override
    public void run() {
        int count = 0;
        while (true) {
            // 根据坦克的方向继续移动
            switch (getDirect()) {
                case 0:
                    moveUp();
                    break;
                case 1:
                    moveRight();
                    break;
                case 2:
                    moveDown();
                    break;
                case 3:
                    moveLeft();
                    break;
            }

            try {
                Thread.sleep(50);
                count++;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            // 随机改变坦克方向
            if (count == 30) {
                setDirect((int)(Math.random()*4));
                count = 0;
            }


            // 退出线程
            if (!isLive) break;
        }
    }
}
